<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<script type="text/javascript">
    var categoryTreeGrid;
    $(function() {
        categoryTreeGrid = $('#categoryTreeGrid').treegrid({
            url : '${path }/category/queryTree',
            idField : 'id',
            treeField : 'name',
            parentField : 'pid',
            fit : true,
            fitColumns : false,
            border : false,
           
            columns : [ [ {
                title : '类目名称',
                field : 'name',
                width : 150
            } ,{
                title : '类目级别',
                field : 'level',
                width : 140
            } ,
             {
                title : '创建时间',
                field : 'gmtCreate',
                width : 150
            },  {
                title : '排序',
                field : 'seq',
                width : 40
            },{
                title : '上级资源ID',
                field : 'pid',
                width : 150,
                hidden : true
            }, {
                title : '状态',
                field : 'status',
                width : 40,
                formatter : function(value, row, index) {
                    switch (value) {
                    case 0:
                        return '正常';
                    case 1:
                        return '停用';
                    }
                }
            }, {
                title : '操作',
                field : 'action',
                width : 130,
                formatter : function(value, row, index) {
                    var str = '';
                        <shiro:hasPermission name="/category/edit">
                            str += $.formatString('<a href="javascript:void(0)" class="resource-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="editCategoryFun(\'{0}\');" >编辑</a>', row.id);
                        </shiro:hasPermission>
                        if(row.level==3){
                        	if(row.status==0){ //正常状态可以停用
		                        <shiro:hasPermission name="/category/stop">
		                            str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
		                            str += $.formatString('<a href="javascript:void(0)" class="resource-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red\'" onclick="stopCategoryFun(\'{0}\');" >停用</a>', row.id);
		                        </shiro:hasPermission>
                        	}else if(row.status==1){//停用状态可以恢复正常
                        		<shiro:hasPermission name="/category/start">
	                            	 str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
	                           		 str += $.formatString('<a href="javascript:void(0)" class="resource-easyui-linkbutton-add" data-options="plain:true,iconCls:\'fi-plus icon-green\'" onclick="startCategoryFun(\'{0}\');" >恢复</a>', row.id);
	                       		</shiro:hasPermission>
                        	}
                        }
                    return str;
                }
            } ] ],
          
            onLoadSuccess:function(data){
                $('.resource-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.resource-easyui-linkbutton-del').linkbutton({text:'停用'});
                $('.resource-easyui-linkbutton-add').linkbutton({text:'恢复'});
            },
            toolbar : '#toolbar_category'
        });
    });
    function success(data){  
  	   $.messager.alert("返回结果","操作成功");  
   }
    function editCategoryFun(id) {
        if (id != undefined) {
            categoryTreeGrid.treegrid('select', id);
        }
        var node = categoryTreeGrid.treegrid('getSelected');
        if (node) {
            parent.$.modalDialog({
                title : '编辑',
                width : 500,
                height : 350,
                href : '${path }/category/editPage?id=' + node.id,
                buttons : [ {
                    text : '确定',
                    handler : function() {
                        parent.$.modalDialog.openner_treeGrid = categoryTreeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
                        var f = parent.$.modalDialog.handler.find('#categoryEditForm');
                        f.submit();
                    }
                } ]
            });
        }
    }

    function stopCategoryFun(id) {
        if (id != undefined) {
            categoryTreeGrid.treegrid('select', id);
        }
        var node = categoryTreeGrid.treegrid('getSelected');
        if (node) {
            parent.$.messager.confirm('询问', '您是否要停用当前类目', function(b) {
                if (b) {
                    progressLoad();
                    $.post('${path }/category/stop', {
                        id : node.id
                    }, function(result) {
                        if (result.success) {
                            parent.$.messager.alert('提示', result.msg, 'info');
                            categoryTreeGrid.treegrid('reload');
                            parent.layout_west_tree.tree('reload');
                        }
                        progressClose();
                    }, 'JSON');
                }
            });
        }
    }
    
    function startCategoryFun(id) {
        if (id != undefined) {
            categoryTreeGrid.treegrid('select', id);
        }
        var node = categoryTreeGrid.treegrid('getSelected');
        if (node) {
            parent.$.messager.confirm('询问', '您是否要开启当前类目', function(b) {
                if (b) {
                    progressLoad();
                    $.post('${path }/category/start', {
                        id : node.id
                    }, function(result) {
                        if (result.success) {
                            parent.$.messager.alert('提示', result.msg, 'info');
                            categoryTreeGrid.treegrid('reload');
                            parent.layout_west_tree.tree('reload');
                        }
                        progressClose();
                    }, 'JSON');
                }
            });
        }
    }
    

    function addCategoryFun() {
        parent.$.modalDialog({
            title : '添加',
            width : 500,
            height : 350,
            href : '${path }/category/addPage',
            buttons : [ {
                text : '添加',
                handler : function() {
                    parent.$.modalDialog.openner_treeGrid = categoryTreeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#categoryAddForm');
                    f.submit();
                }
            } ]
        });
    }
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false"  style="overflow: hidden;">
        <table id="categoryTreeGrid"></table>
    </div>
</div>
<div id="toolbar_category" style="display: none;">
    <shiro:hasPermission name="/category/add">
        <a onclick="addCategoryFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-green'">添加</a>
    </shiro:hasPermission>
</div>